<?php

namespace Home\Controller;

use Think\Controller;
use Common\Util\LanzSMSDirectSendSMS;

class CrontabController extends Controller{
	
	public function index() {
		echo 'success';
	}
	
	/**
	 * 提前半小时通知美容师
	 */
	public function beauticianServiceBefore() {
		$OrderModel = D('Order');
		$time = strtotime(date('Y-m-d H', strtotime('+1 hour')).':00:00');
		
		$list = $OrderModel->alias('o')->field(array('o.id', 'b.name', 'o.beautician_id','b.mobile', 'o.order_time', 'o.service_name'))
			->join('left join '.C('DB_PREFIX').'beautician as b on o.beautician_id=b.id')
			->where(array('o.order_time' => $time, 'o.status' => 1, 'o.use_time' => 0, 'not exists(select * from '.C('DB_PREFIX').'message_notice_record where target_id=o.beautician_id and order_id=o.id and type="before")'))
			->select();
		
		if ($list) {
			$setting = C('MESSAGE_TEMP');
			$template = $setting ['before_service'];
				
			$LchiObj = new LanzSMSDirectSendSMS();
			foreach ($list as $key => $value) {
				$content = sprintf($template , $value['name'], $value ['service_name']);
				$result = sendSMS($LchiObj, $value['mobile'], $content);
				
				$params = array(
					'mobile' => $value ['mobile'],
					'content' => $content,
					'type' => 'before',
					'target_id' => $value ['beautician_id'],
					'order_id' => $value ['id'],
					'status' => $result?1:0
				);
				\Think\hook::listen('message_record', $params);
			}
		}
		
		echo '操作已完成';
	}
	
	/**
	 * 服务开始5分钟未点击开始订单
	 */
	public function beauticianServiceStart() {
		$OrderModel = D('Order');
		$time = strtotime(date('Y-m-d H', time()).':00:00');
		
		$list = $OrderModel->alias('a')->field(array('b.mobile', 'o.order_time', 'o.service_name'))
		->join('left join '.C('DB_PREFIX').'beautician as b on o.beautician_id=b.id')
		->where(array('o.order_time' => $time, 'o.status' => 1, 'o.use_time' => 0))
		->select();
		
		if ($list) {
// 			$setting = getGlobleConfig();
// 			$template = D('MessageTemplate')->getContent($setting ['after_service_start']);
			$setting = C('MESSAGE_TEMP');
			$template = $setting ['after_service_start'];
			
			$LchiObj = new LanzSMSDirectSendSMS();
			foreach ($list as $key => $value) {
				$content = sprintf($template ['content'], $value ['service_name'], date('m月d日 H：i', $value ['order_time']));
				sendSMS($LchiObj, $value ['mobile'], $content);
			}
		}
		
		echo '操作已完成';
	}
	
	/**
	 * 给未点击完成的订单发送短信
	 */
	public function beauticianServiceComplete() {
		$OrderModel = D('Order');
		$time = strtotime(date('Y-m-d H', time()).':00:00');
		
		$list = $OrderModel->alias('o')->field(array('b.mobile', 'o.service_time', 'o.use_time', 'o.service_name'))
			->join('left join '.C('DB_PREFIX').'beautician as b on o.beautician_id=b.id')
			->where(array('o.order_time' => $time, 
				'o.status' => 3, 
				'o.use_time' => array('neq', 0),
				'o.use_end_time' => 0,
			))
			->select();
		
		if ($list) {
// 			$setting = getGlobleConfig();
// 			$template = D('MessageTemplate')->getContent($setting ['after_service_complete']);
			$setting = C('MESSAGE_TEMP');
			$template = $setting ['after_service_complete'];
			
			$LchiObj = new LanzSMSDirectSendSMS();
			foreach ($list as $key => $value) {
				if ((time() - ($value ['use_time'] + $value ['service_time'] * 60))/ 60 > 30 ) {
					$content = sprintf($template ['content'], $value ['service_name']);
					sendSMS($LchiObj, $value ['mobile'], $content);
				}
			}
		}
		
		echo '操作已完成';
	}
}

?>